const { addArticleTypeService, getArticleService, updateArticleService } = require('../service/articletypemanage.service')
class ArticleTypeController {
    // add
    async addArticleType(ctx, next) {
       
        // 1.获取数据
        const { type } = ctx.request.body
        // 2.操作数据库
        try {
            // 调用service层最好加try catch
            const { data } = await addArticleTypeService({ type })
            // 3.返回给客户端
            ctx.body = {
                code: 1000,
                result: 'success'
            }
        } catch (err) {
            console.log(err)
            ctx.app.emit('error', userRegisterError, ctx)
        }
    }
    // delete update
    async updateArticleType(ctx, next) {
        const { type, new_type='', is_delete = '' } = ctx.request.body
        console.log(type, new_type, is_delete)
        try {
            // 调用service层最好加try catch
            const res = await updateArticleService({ type, new_type, is_delete })
            // 3.返回给客户端
            ctx.body = {
                code: 1000,
                result: res
            }
        } catch (err) {
            console.log(err)
            ctx.app.emit('error', userRegisterError, ctx)
        }
    }
    // get
    async getArticleType(ctx, next) {
        const { currentPage = 1, pageSize = 10,type='',createdFrom='',createdTo='' } = ctx.request.query
        // 2.操作数据库
        try {
            // 调用service层最好加try catch
            const { data } = await getArticleService({ currentPage, pageSize,type,createdFrom,createdTo  })
            // 3.返回给客户端
            ctx.body = {
                code: 1000,
                data,
                current: currentPage,
                pageSize: pageSize
            }
        } catch (err) {
            console.log(err)
            ctx.app.emit('error', ctx)
        }
    }
}

// 导出实例化的对象
module.exports = new ArticleTypeController()